<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="HandheldFriendly" content="true" />
		<meta name="MobileOptimized" content="320" />
		<title>Hello H5+</title>
		<script type="text/javascript" src="../../../../js/common1.js"></script>
		<script type="text/javascript" src="../../../../js/mui.min.js"></script>
		<script type="text/javascript" src="../../../../js/jquery-1.7.2.min.js"></script>
		<link rel="stylesheet" href="../../../../css/mui.min.css" type="text/css" charset="utf-8" />
		<link rel="stylesheet" href="../../../../css/allApp.css" />
		<style type="text/css">
			#bcid {
				width: 100%;
				position: absolute;
				top: 48px;
				/*height: 40%;*/
				bottom: 100px;
				text-align: center;
				z-index: -1;
			}
			
			.tip {
				color: #FFFFFF;
				font-weight: bold;
				text-shadow: 0px -1px #103E5C;
				
			}
			
			header {
				width: 100%;
				height: 44px;
				position: absolute;
				top: 0px;
				line-height: 44px;
				text-align: center;
				color: #FFF;
			}
			
			.fbt {
				width: 50%;
				height: 100%;
				background-color: #FFCC33;
				float: left;
			}
			
			.fbt:active {
				-webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.5);
				box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.5);
			}
			
			.mui-bar-nav {
				background: rgba(0, 0, 0, .5);
			}
			
			.mui-title {
				color: #fff;
			}
			.torch {
				width: 100%;
				position: fixed;
				height: 90px;
				bottom: 0;
				z-index: 99999;
				text-align: center;
				background-color: rgba(0,0,0,.5);
				color: #fff;
				font-size: 14px;
			}
			
			.torch .icon-torch {
				width: 20px;
				height: 30px;
			}
			.torch .sx-icon{
				width: 50px;
				height: 50px;
				text-align: center;
				border-radius: 50%;
				background: #fff;
				display: inline-block;
				line-height: 50px;
				margin-bottom: 5px;
			}
			.torch img {
				width: 20px;
				height: 25px;
				vertical-align: middle;
			}
		</style>
	</head>

	<body style="background-color: #000000;">
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">二维码</h1>
			<a class=" mui-icon-right-nav mui-pull-right" onclick="scanPicture()">相册</a>
		</header>
		<div id="bcid">
			<div style="height:40%"></div>
			<p class="tip">...载入中...
			
			</p>
			<div id="torch" class="torch">
				<i class="sx-icon"><img src="./../../img/torch.png" id="img"/></i><br><span class="txt">开启</span>手电筒
			</div>
			
		</div>
		<script type="text/javascript">
			mui.init()
			var ws = null,
				wo = null;
			var scan = null,
				isOpen = false, // 闪光灯是否开启
				domready = false;
			var filter;//扫码格式 空为全类型	
			var scanSendCodeValue;
			// H5 plus事件处理
			function plusReady() {
				if(ws || !window.plus || !domready) {
					return;
				}
				// 获取窗口对象
				ws = plus.webview.currentWebview();
				wo = ws.opener();
				// 开始扫描
				ws.addEventListener('show', function() {
					var styles = {
						frameColor: "#fff",
						scanbarColor: "#fff",
						background: ""
					}
					scan = new plus.barcode.Barcode('bcid',filter,styles);
					scan.onmarked = onmarked;
					scan.start({
						conserve: true,
						filename: '_doc/barcode/'
					});
				}, false);
				// 显示页面并关闭等待框
				ws.show('pop-in');
				
				//				wo.evalJS('closeWaiting()');
			}
			if(window.plus) {
				plusReady();
			} else {
				document.addEventListener('plusready', plusReady, false);
			}
			// 监听DOMContentLoaded事件
			document.addEventListener('DOMContentLoaded', function() {
				domready = true;
				plusReady();
			}, false);
			// 二维码扫描成功
			function onmarked(type, result, file) {
				switch(type) {
					case plus.barcode.QR:
						type = 'QR';
						break;
					/*case plus.barcode.EAN13:
						type = 'EAN13';
						break;
					case plus.barcode.EAN8:
						type = 'EAN8';
						break;*/
					default:
						type = '其它' + type;
						break;
				}
				if(type=='QR'){
					result = result.replace(/\r\n/g, '');
					console.log('二维码'+result);
					CurentTime();
					mui.openWindow({
						url: 'pinlv.html',
						id: 'pinlv.html',
						extras: {
							scan: result
						}
					});
				}else{
					console.log('1111');
					scan.cancel();
					scan.start();
				}
				
				
				
//					plus.webview.currentWebview().close();
					

			}
			// 开启闪光灯
			var actionTxt = mui('#torch .txt')[0];
			var imgSrc =$('#torch #img').attr('src');
			mui('#torch')[0].addEventListener('tap', function () {
				isOpen = !isOpen;
				actionTxt.innerHTML = isOpen ? '关闭' : '开启';
				imgSrc = isOpen ? './img/torchH.png' : './img/torch.png';
				$('#torch #img').attr('src',imgSrc)
				scan.setFlash(isOpen);
			})

			// 从相册中选择二维码图片 
			function scanPicture() {
				plus.gallery.pick(function(path) {
					plus.barcode.scan(path, onmarked, function(error) {
						plus.nativeUI.alert('无法识别此图片');
					});
				}, function(err) {
					console.log('Failed: ' + err.message);
				});
			}
			
			//获取当前时间
			function CurentTime(){ 
			    var now = new Date();
			   
			    var year = now.getFullYear();       //年
			    var month = now.getMonth() + 1;     //月
			    var day = now.getDate();            //日
			   
			    var hh = now.getHours();            //时
			    var mm = now.getMinutes();          //分
			    var ss = now.getSeconds();          //秒
			   
			    var clock = year + "-";
			   
			    if(month < 10)
			        clock += "0";
			   
			    clock += month + "-";
			   
			    if(day < 10)
			        clock += "0";
			       
			    clock += day + " ";
			   
			    if(hh < 10)
			        clock += "0";
			       
			    clock += hh + ":";
			    if (mm < 10) clock += '0'; 
			    clock += mm + ":";
			    
			    if (ss < 10) clock += '0'; 
			    clock += ss;
			    localStorage.setItem("curentTime",clock);
			    return(clock); 
			} 
		</script>
	</body>

</html>